package com.example.teesystem.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.teesystem.common.Permission.PermissionCheck;
import com.example.teesystem.common.utils.PermissionUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.common.utils.TimeUtil;
import com.example.teesystem.entity.vo.IssueFeedback.IssueFeedbackExcel;
import com.example.teesystem.entity.vo.IssueFeedback.IssueFeedbackInsertVo;
import com.example.teesystem.entity.vo.IssueFeedback.IssueFeedbackSelectVo;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.IssueFeedbackService;
import lombok.Cleanup;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author pxr
 * @data 2022/3/31 16:26
 */
@RestController
@RequestMapping(value = "/tee/issueFeedback" , produces = "application/json; charset=utf-8" )
@RequiredArgsConstructor
public class IssueFeedbackController {
    private final IssueFeedbackService issueFeedbackService;

    /**
     * 提交题目反馈
     * @param issueFeedbackInsertVo
     * @return
     */
    @PostMapping(value = "/submit")
    public Response submit(@Valid @RequestBody IssueFeedbackInsertVo issueFeedbackInsertVo){
        try{
            if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.ISSUE_MANAGE, issueFeedbackInsertVo.getCourseId())) {
                return Response.error(ResultCode.NO_PERMISSION.getMsg(), ResultCode.NO_PERMISSION.getCode());
            }
            return issueFeedbackService.submit(issueFeedbackInsertVo);
        }catch (Exception e){
            return Response.error(ResultCode.INSERT_ERROR.getMsg(), ResultCode.INSERT_ERROR.getCode());
        }
    }

    /**
     * 分页获取题目反馈
     * @param issueFeedbackSelectVo
     * @return
     */
    @PostMapping(value = "/getIssueFeedback")
    @PermissionCheck(PermissionUtil.ISSUE_MANAGE)
    public Response getIssueFeedback(@Valid @RequestBody IssueFeedbackSelectVo issueFeedbackSelectVo){
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.ISSUE_MANAGE, issueFeedbackSelectVo.getCourseId())) {
            return Response.error(ResultCode.NO_PERMISSION.getMsg(), ResultCode.NO_PERMISSION.getCode());
        }
        return Response.ok(issueFeedbackService.getIssueFeedback(issueFeedbackSelectVo));
    }

    /**
     * 处理题目反馈
     * @param map
     * @return
     */
    @PostMapping(value = "/handleFeedback")
    @PermissionCheck(PermissionUtil.ISSUE_MANAGE)
    public Response handleFeedback(@RequestBody Map<String,String> map){
        int id,courseId;
        try{
            id = Integer.parseInt(map.get("id"));
            courseId = Integer.parseInt(map.get("courseId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR.getMsg(), ResultCode.PARAMETER_ERROR.getCode());
        }
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.ISSUE_MANAGE, courseId)) {
            return Response.error(ResultCode.NO_PERMISSION.getMsg(), ResultCode.NO_PERMISSION.getCode());
        }
        return issueFeedbackService.handleFeedback(MyHandlerInterceptor.getUid(), id);
    }

    /**
     * 题库excel导出
     * @param map
     * @param response
     * @return
     */
    @RequestMapping(value = "/outPutFeedback")
    @PermissionCheck(PermissionUtil.ISSUE_MANAGE)
    public Response outPutFeedback(@RequestBody Map<String,String> map, HttpServletResponse response){
        int isHandle,courseId;
        try{
            isHandle = Integer.parseInt(map.get("isHandle"));
            courseId = Integer.parseInt(map.get("courseId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR.getMsg(), ResultCode.PARAMETER_ERROR.getCode());
        }
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.ISSUE_MANAGE, courseId)) {
            return Response.error(ResultCode.NO_PERMISSION.getMsg(), ResultCode.NO_PERMISSION.getCode());
        }
        List<IssueFeedbackExcel> issueFeedbackExcelList = issueFeedbackService.getIssueFeedbackExcelList(isHandle,courseId);
        try{
            @Cleanup
            ServletOutputStream out = response.getOutputStream();
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS,true);
            String fileName = "IssueFeedback_"+courseId+"_"+ TimeUtil.getSecondTimestamp();
            Sheet sheet = new Sheet(1,0,IssueFeedbackExcel.class);
            //设置自适应宽度
            sheet.setAutoWidth(Boolean.TRUE);
            // 第一个 sheet 名称
            sheet.setSheetName("sheet1");
            writer.write(issueFeedbackExcelList, sheet);
            //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
            response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
            writer.finish();
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            out.flush();
        }catch (Exception e){
            return Response.error(ResultCode.File_OUTPUT_FAILURE.getMsg(), ResultCode.File_OUTPUT_FAILURE.getCode());
        }
        return Response.ok();
    }

}
